// packageA/pages/address/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressList: [
            {
              id: 1,
              name: "步游",
              phone: "13813357897",
              detail: "江苏 宿迁市 沭阳县 沭阳图书城公寓文创楼1901",
              tag: "公司",
              isDefault: true
            },
            // 更多地址数据...
          ],
          showPopup: false,
          isEdit: false,
          editIndex: -1,
          newAddress: {
            name: "",
            phone: "",
            detail: "",
            tag: "",
            isDefault: false
          },
          region: ['江苏省', '宿迁市', '沭阳县'],
          tags: ["学校", "家", "公司", "购物", "秒送/外卖", "自定义"]
    },
    addAddress() {
        this.setData({
          showPopup: true,
          isEdit: false,
          newAddress: {
            name: "",
            phone: "",
            detail: "",
            tag: "",
            isDefault: false
          }
        });
      },
    
      closePopup() {
        this.setData({
          showPopup: false
        });
      },
    
      inputName(e) {
        this.setData({
          "newAddress.name": e.detail.value
        });
      },
    
      inputPhone(e) {
        this.setData({
          "newAddress.phone": e.detail.value
        });
      },
    
      bindRegionChange(e) {
        this.setData({
          region: e.detail.value
        });
      },
    
      inputDetail(e) {
        this.setData({
          "newAddress.detail": e.detail.value
        });
      },
    
      toggleDefault() {
        this.setData({
          "newAddress.isDefault":!this.data.newAddress.isDefault
        });
      },
    
      selectTag(e) {
        const tag = e.currentTarget.dataset.tag;
        this.setData({
          "newAddress.tag": tag
        });
      },
    
      confirmAddOrEdit() {
        if (this.data.isEdit) {
          // 修改地址逻辑
          const newList = this.data.addressList;
          newList[this.data.editIndex] = this.data.newAddress;
          this.setData({
            addressList: newList,
            showPopup: false
          });
        } else {
          // 新增地址逻辑
          const newAddress = {
            id: Date.now(),
            ...this.data.newAddress
          };
          this.setData({
            addressList: [...this.data.addressList, newAddress],
            showPopup: false
          });
        }
      },
    
      editAddress(e) {
        const index = e.currentTarget.dataset.index;
        this.setData({
          showPopup: true,
          isEdit: true,
          editIndex: index,
          newAddress: {...this.data.addressList[index]}
        });
      },
    
      deleteAddress(e) {
        const index = e.currentTarget.dataset.index;
        const newList = this.data.addressList;
        newList.splice(index, 1);
        this.setData({
          addressList: newList
        });
      },
    
      copyAddress(e) {
        const index = e.currentTarget.dataset.index;
        const address = this.data.addressList[index];
        wx.setClipboardData({
          data: `收货人：${address.name}，电话：${address.phone}，地址：${address.detail}`,
          success() {
            wx.showToast({
              title: '复制成功',
            });
          }
        });
      },
    
      setDefault(e) {
        const index = e.currentTarget.dataset.index;
        const newList = this.data.addressList;
        newList.forEach((item, i) => {
          item.isDefault = i === index;
        });
        this.setData({
          addressList: newList
        });
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})